from datetime import datetime
from elasticsearch_dsl import Document, Date, Integer, Keyword, Text, connections,Completion

# Define a default Elasticsearch client
connections.create_connection(hosts=['localhost'])

class ArticleType(Document):
	#伯乐在线文章类型
	#不需要保持所有字段
	suggest=Completion(analyzer="ik_max_word")
	title = Text(
		analyzer="ik_max_word"
	)
	created_at = Date()
	url = Keyword()
	url_object_id = Keyword()
	cover_image = Keyword()
	cover_image_path = Keyword()
	praise_nums = Integer()
	comment_nums = Integer()
	fav_nums = Integer()
	tags = Text(analyzer="ik_max_word")
	content = Text(analyzer="ik_max_word")

	def save(self, **kwargs):
		# self.lines = len(self.body.split())
		return super(ArticleType, self).save(**kwargs)

	class Index:
		name="jobble"
		settings = {
			"number_of_shards": 2,
		}

if __name__=="__main__":
	# 根据类创建mapping
	ArticleType.init()

	# article = ArticleType(meta={'id': 1}, title='Hello world!')
	# article.save()